﻿using System;
using System.Collections.Concurrent;
using System.Configuration;
using System.Data.Entity.SqlServer;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using In.Api.Dto.HisWs;
using Newtonsoft.Json;
using Out.TaskRunner.RmqWorkers;
using OutDbContext;
using Quartz;
using Quartz.Impl;
using User.Api.HttpClients;

namespace Out.TaskRunner
{
    class Program
    {
        static void Main(string[] args)
        {
            // 注册微信
            //SenparcWeixinConfig.Register();

            // 注册 MQ 消费 任务
            Task task;
            var tasks = new ConcurrentBag<Task>();
            var cts = new CancellationTokenSource();
            var ct = cts.Token;

            // 解锁号源
            task = Task.Factory.StartNew(() => new OrderUnlockingWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 取消预约
            task = Task.Factory.StartNew(() => new OrderUnapptingWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 支付超时
            task = Task.Factory.StartNew(() => new OrderPayingTimeoutWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 执行订单（院内取号，缴费等）
            task = Task.Factory.StartNew(() => new OrderPickupingWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 订单执行状态检查
            task = Task.Factory.StartNew(() => new OrderPickupingChkWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 订单执行失败退费
            task = Task.Factory.StartNew(() => new OrderRefundingPickupFailedWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 退号
            task = Task.Factory.StartNew(() => new OrderUnpickupingWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 退号状态检查
            task = Task.Factory.StartNew(() => new OrderUnpickupingChkWork().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 退号退费
            task = Task.Factory.StartNew(() => new OrderRefundingUnpickupSucceedWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 用户通知消息
            task = Task.Factory.StartNew(() => new UserMessageWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);
            // 院内通知消息
            task = Task.Factory.StartNew(() => new HospMessageWorker().Start(ct), ct, TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
            tasks.Add(task);

            IScheduler sched = null;
            if (ConfigurationManager.AppSettings["StartQuartz"] == "true")
            {
                //启动 Quartz
                ISchedulerFactory sf = new StdSchedulerFactory();
                sched = sf.GetScheduler();

                sched.Start();
            }

            //Test();
            
            Console.WriteLine("Out.TaskRunner 服务程序已启动！");
            Console.WriteLine("输入 exit 来终止程序.");
            while (true)
            {
                if (Console.ReadLine() == "exit")
                {
                    cts.Cancel();

                    Task.WaitAll(tasks.ToArray());

                    sched?.Shutdown(true);
                    break;
                }
            }
        }

    }
}
